package com.example.summary.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * Created by wzn on 2019/1/10.
 */
public class TestCyclicBarrier {

//    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
//        CountDownLatch countDownLatch = new CountDownLatch(1);
//        CyclicBarrier cyclicBarrier = new CyclicBarrier(3, () -> {
//            System.out.println("考试结束");
//        });
//
//        Runnable runnable = () -> {
//            try {
//                System.out.println(Thread.currentThread().getName()+"等待考试开始");
//                countDownLatch.await();
//                System.out.println(Thread.currentThread().getName()+"开始答题");
//                Thread.sleep((int) (Math.random() * 10 + 1) * 1000);
//                System.out.println(Thread.currentThread().getName() + "交卷了");
//                cyclicBarrier.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            } catch (BrokenBarrierException e) {
//                e.printStackTrace();
//            }
//        };
//
//        new Thread(runnable).start();
//        new Thread(runnable).start();
//
//        System.out.println("考生入场准备考试");
//        Thread.sleep((int) (Math.random() * 10 + 1) * 1000);
//        System.out.println("考试开始了");
//        countDownLatch.countDown();
//        cyclicBarrier.await();
//
//    }
}
